var prefix = "/sys/job";

$(document).ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: prefix + "/user",
        data: $('#jobUserForm').serialize(),// 你的formid
        async: false,
        error: function () {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code === 0) {
                parent.layer.msg("任命成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            } else if (data.code === 200) {
                $("#updateId").val(123456);
                layer.confirm(data.msg, {
                    btn: ['确定', '取消']
                }, function () {
                    $("#userIds").val(data.data);
                    save();
                }, function () {
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);
                });
            } else {
                parent.layer.msg(data.msg);
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#jobUserForm").validate({
        rules: {
            userNames: {
                required: true
            }
        },
        messages: {
            userNames: {
                required: icon + "还未选择用户！！"
            }
        }
    })
}

var openUser = function () {
    layer.open({
        type: 2,
        title: "选择人员",
        area: ['300px', '450px'],
        content: "/sys/user/treeView"
    })
};

function loadUser(userIds, userNames) {
    $("#userIds").val(userIds.substring(0, userIds.length - 1));
    $("#userNames").val(userNames.substring(0, userNames.length - 1));
}